/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "gate.h"
#include <QGraphicsItem>
#include <QColor>
#include <QList>
#include <QVarLengthArray>
#include <QPainter>
#include <QStyleOptionGraphicsItem>

Gate::Gate(const QColor& cColor_, QSize& cSize_)
{
    m_nWidth  = cSize_.width();
    m_nHeight = cSize_.height();
    m_cColor   = cColor_;
    setZValue((m_nWidth + m_nHeight) % 2);

    setFlags(ItemIsSelectable | ItemIsMovable);
    setAcceptsHoverEvents(true);
}

QRectF Gate::boundingRect() const
{
    return QRectF(0, 0, m_nWidth , m_nHeight);
}

QPainterPath Gate::shape() const
{
    QPainterPath path;
    path.addRect(0, 0, m_nWidth , m_nHeight);
    return path;
}

void Gate::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget); // to tell the compiler this parameter is not used, do not expose warning
    //******** to change the background color of when mouse is rect
    QColor fillColor = (option->state & QStyle::State_Selected) ? m_cColor.dark(150) : m_cColor;
    if (option->state & QStyle::State_MouseOver)
        fillColor = fillColor.light(125);
    //******** get the enlarged rate, and by which to determine whether draw the details
    //******** the real display size diliver real size (in pixels)
    //const qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
    QPen _OldPen = painter->pen();
    QPen _NewPen = _OldPen ;

    _NewPen.setWidth(1);
    _NewPen.setColor(fillColor);
    painter->setPen(_NewPen);

    double nTmp = m_nHeight/2 + 0.5;
    QVarLengthArray<QLineF, 2> lines;
    lines.append(QLineF(0 , 0 , 0 , m_nHeight));
    lines.append(QLineF(m_nWidth , 0 , m_nWidth , m_nHeight));
    painter->drawLines(lines.data() , 2);
    _NewPen.setWidth(2);
    painter->setPen(_NewPen);
    painter->drawLine(QPointF(1 , nTmp) , QPointF(m_nWidth - 1 , nTmp));
    painter->setPen(_OldPen);
}

void Gate::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void Gate::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
}

void Gate::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}
